package com.exchange.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exchange.entity.model.AdminTokenModel;
import com.exchange.entity.pojo.User;
import com.exchange.entity.model.TokenModel;
import com.exchange.entity.vo.UserInfo;
import com.exchange.mapper.UserMapper;
import com.exchange.service.UserService;
import com.exchange.until.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-04-25 20:02:10
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<AdminTokenModel> login(User user) {
        log.info("user------>{}", user);
        //数据校验
        if (ObjectUtil.isEmpty(user)) {
            return Result.error("用户名或者密码不能为空");
        }

        //校验手机号和密码
        if (ObjectUtils.isEmpty(user.getMobile())) {
            return Result.error("手机号不能为空");
        }
        //根据手机号查找出来salt
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", user.getMobile());
        User arUser = userMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(arUser)) {
            return Result.error("用户不存在");
        }
        String salt = arUser.getSalt();
        //密码和salt进行加密
        String password = DigestUtils.md5DigestAsHex((user.getPassword() + salt).getBytes());
//        System.out.println(password);
        QueryWrapper query = new QueryWrapper<>();
        query.eq("password", password);
        query.eq("mobile", user.getMobile());
        //查询
        User users = userMapper.selectOne(query);
        if (ObjectUtils.isEmpty(users)) {
            return Result.error("用户名或者密码错误");
        }
        //生成token保存到redis  把用户信息保存到redis
        String token = IdUtil.simpleUUID().replace("-", "");
        //token有效时长30分钟
        redisTemplate.opsForValue().set("user:login" + token, JSON.toJSONString(users), 30, TimeUnit.MINUTES);
        AdminTokenModel adminTokenModel = new AdminTokenModel();
        adminTokenModel.setToken(token);
        adminTokenModel.setRefreshToken("");
        System.err.println(adminTokenModel);
        return Result.success(adminTokenModel);
    }
    @Override
    public User selectPersonal(String token) {
        String userinfo = this.redisTemplate.opsForValue().get("user:login"+token);
        System.err.println("userinfo：" + userinfo);
        User user = JSON.parseObject(userinfo, User.class);
        String mobile = user.getMobile();
        User user1 = this.userMapper.selectPersonal(mobile);
        return user1;
    }
}




